package com.kly.cashmall.module.order;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarFragment;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.LoanListBean;
import com.kly.cashmall.event.HomeEvent;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.order.adapter.LoanListAdapter;
import com.kly.cashmall.module.order.presenter.LoanListPresenter;
import com.kly.cashmall.module.order.presenter.LoanListViewer;
import com.kly.cashmall.utils.data.TextUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * @author
 * desc:订单列表
 */
public class LoanListFragment extends BaseBarFragment implements LoanListViewer {

    @PresenterLifeCycle
    LoanListPresenter presenter = new LoanListPresenter(this);

    public static final String KEY_TYPE_ID = "KEY_TYPE_ID";

    private RecyclerView mOrderList;

    private LoanListAdapter mOrderListAdapter;

    private String mOrderType;
    private int currentPage = 1;
    private SmartRefreshLayout mSmartRefreshLayout;
    private boolean hasNextPage = false;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_loan_list;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setActionBar(false);
        analyseArguments(getArguments());
        initView();
        EventBus.getDefault().register(this);

    }

    private void analyseArguments(Bundle arguments) {
        if (arguments != null) {
            mOrderType = arguments.getString(KEY_TYPE_ID);
        }
    }

    @Override
    protected boolean inFragmentPageAdapter() {
        return true;
    }

    private void initView() {
        mSmartRefreshLayout = bindView(R.id.smartRefreshLayout);

        mOrderList = bindView(R.id.recycler_view);
        mOrderListAdapter = new LoanListAdapter();
        mOrderList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mOrderList.setAdapter(mOrderListAdapter);
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (hasNextPage) {
                    currentPage++;
                    presenter.getOrderList(mOrderType, currentPage);
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                currentPage = 1;
                presenter.getOrderList(mOrderType, currentPage);
            }
        });
        mOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            BaseActionHelper.with(getContext()).handleAction(mOrderListAdapter.getData().get(position).jumpUrl);
        });
    }

    @Override
    protected void loadData() {
        presenter.getInitialOrderList(mOrderType, currentPage);
    }

    @Override
    public void setOrderListRefreshing(LoanListBean orderDetailBeans) {
        bindView(R.id.status_empty_view, false);
        bindView(R.id.smartRefreshLayout, true);
        bindView(R.id.marquee_view_container, !TextUtil.isEmpty(orderDetailBeans.getNoticeMessage()));
        if(!TextUtil.isEmpty(orderDetailBeans.getNoticeMessage())){
            bindText(R.id.tv_order_tip,orderDetailBeans.getNoticeMessage());
        }

        hasNextPage = orderDetailBeans.isHasNextPage();
        mSmartRefreshLayout.setEnableLoadMore(hasNextPage);

        if (mSmartRefreshLayout.getState() == RefreshState.Refreshing) {
            mSmartRefreshLayout.finishRefresh();
        }
        mOrderListAdapter.setList(orderDetailBeans.getRepaymentRecords());
    }

    @Override
    public void setOrderListLoadmore(LoanListBean orderDetailBeans) {

        mSmartRefreshLayout.setEnableLoadMore(orderDetailBeans.isHasNextPage());
        if (mSmartRefreshLayout.getState() == RefreshState.Loading) {
            mSmartRefreshLayout.finishLoadMore();
        }
        mOrderListAdapter.addData(orderDetailBeans.getRepaymentRecords());
        mOrderListAdapter.notifyDataSetChanged();
    }

    @Override
    public void setOrderListFailRefreshing(String noticeMessage) {
        bindView(R.id.marquee_view_container, !TextUtil.isEmpty(noticeMessage));
        if(!TextUtil.isEmpty(noticeMessage)){
            bindText(R.id.tv_order_tip,noticeMessage);
        }

        bindText(R.id.empty_title, getString(R.string.no_record));
        bindView(R.id.status_empty_view, true);
        bindView(R.id.smartRefreshLayout, false);
    }

    @Override
    public void setOrderListFailLoadmore() {
        currentPage--;
    }

    public void compareToRefresh() {
        currentPage = 1;
        presenter.getOrderList(mOrderType, currentPage);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHomeEvent(HomeEvent homeEvent) {
        presenter.getOrderList(mOrderType, currentPage);
    }

    @Override
    protected void willDestroy() {
        super.willDestroy();
        EventBus.getDefault().unregister(this);
    }
}
